package com.example.visitor.converter;

import com.example.visitor.vo.VisitorVO;
import com.example.visitor.mapper.InterMapper;
import com.example.visitor.mapper.PartMapper;
import com.example.visitor.pojo.Inter;
import com.example.visitor.pojo.Part;
import com.example.visitor.pojo.Visitor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  SongPeng
 * @date  2021/11/11 20:48
 * @version 1.0
 */


@Component
public class VisitorConverter {
    @Autowired
    private InterMapper interMapper;
    @Autowired
    private PartMapper partMapper;

    /**
     * 转voList
     * @param visitors
     * @return
     */
    public  List<VisitorVO> converterToVisitorVOList(List<Visitor> visitors){
        List<VisitorVO> visitorVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(visitors)){
            for (Visitor visitor : visitors) {
                VisitorVO visitorVO = converterToVisitorVO(visitor);
                visitorVOS.add(visitorVO);
            }
        }
        return visitorVOS;
    }

    /**
     * 转vo
     * @return
     */
    public  VisitorVO converterToVisitorVO(Visitor visitor){
        VisitorVO visitorVO=new VisitorVO();
        BeanUtils.copyProperties(visitor,visitorVO);
        Inter inter = interMapper.selectByPrimaryKey(visitor.getInterId());
        if(inter!=null&&inter.getInterName()!=null){
            visitorVO.setInterId(inter.getInterId());
            visitorVO.setInterAge(inter.getInterAge());
            visitorVO.setInterName(inter.getInterName());
            visitorVO.setInterPhone(inter.getInterPhone());
            visitorVO.setInterBirth(inter.getInterBirth());
        }
        if(partMapper.selectByPrimaryKey(inter.getPartId())!=null){
            Part part=partMapper.selectByPrimaryKey(inter.getPartId());
            visitorVO.setPartId(part.getPartId());
            visitorVO.setPartName(part.getPartName());
        }

        return visitorVO;
    }
}
